package com.shengdai.urms.core.model;

/**
 * 权限管理内容为5个字节 先后次序表示：管理-查看-新增-修改-删除，1-表示有此项功能，0-表示没有此项功能 11111-表示有所有功能，00000-表示没有任何功能
 *
 */
public class UmUserRole implements java.io.Serializable {

	private static final long	serialVersionUID	= 30030016L;
	public static final String	ALL_SYS_FLAG		= "_ALL";

	private String				orgid;								// 组织ID
	private String				roleids;							// 角色ID
	private String				orgtype;							// 组织类型
	private String				orgrange;							// 组织范围
	private String				admself;							// 管理全部角色
	private String				admglobal;							// 管理全局角色
	private String				admsystem;							// 管理本系统角色
	private String				systems;							// 系统
	private String				includeorg;						// 包含组织

	public String getAdmself() {
		return admself;
	}

	public void addRoleid(String roleid) {
		if (roleids == null || roleids.length() == 0)
			roleids = roleid;
		else if (roleids.indexOf(roleid) == -1) {
			if (roleid.startsWith(","))
				roleids = roleids + roleid;
			else
				roleids = roleids + "," + roleid;
		}
	}

	public void delRoleid(String roleid) {
		if (roleids == null || roleid.length() == 0)
			return;
		roleids = roleids.replaceAll(roleid, "");
		roleids = roleids.replaceAll(",,", ",");
		if (",".equals(roleids))
			roleids = "";
		else if (roleids.startsWith(","))
			roleids = roleids.substring(1);
		else if (roleids.endsWith(","))
			roleids = roleids.substring(0, roleids.length() - 1);
	}

	public void addSystem(String syscode) {
		if (systems == null || systems.length() == 0)
			systems = syscode;
		else if (systems.indexOf(syscode) == -1)
			systems = systems + "," + syscode;
	}

	public void delSystem(String syscode) {
		if (systems == null || systems.length() == 0)
			return;
		systems = systems.replaceAll(syscode, "");
		systems = systems.replaceAll(",,", ",");
	}

	public String getAdmglobal() {
		return admglobal;
	}

	public void setAdmglobal(String admglobal) {
		this.admglobal = admglobal;
	}

	public String getAdmsystem() {
		return admsystem;
	}

	public void setAdmsystem(String admsystem) {
		this.admsystem = admsystem;
	}

	public String getOrgid() {
		return orgid;
	}

	public void setOrgid(String orgid) {
		this.orgid = orgid;
	}

	public String getOrgrange() {
		return orgrange;
	}

	public void setOrgrange(String orgrange) {
		this.orgrange = orgrange;
	}

	public String getOrgtype() {
		return orgtype;
	}

	public void setOrgtype(String orgtype) {
		this.orgtype = orgtype;
	}

	public String getRoleids() {
		return roleids;
	}

	public void setRoleids(String roleids) {
		this.roleids = roleids;
	}

	public String getSystems() {
		return systems;
	}

	public void setSystems(String systems) {
		this.systems = systems;
	}

	public void setAdmself(String admself) {
		this.admself = admself;
	}

	public boolean isAddrglobal() {
		return hasRight(admglobal, 0) || hasRight(admglobal, 2);
	}

	public boolean isAddrsys() {
		return hasRight(admsystem, 0) || hasRight(admsystem, 2);
	}

	public boolean isAddrself() {
		return hasRight(admself, 0) || hasRight(admself, 2);
	}

	public boolean isAdmrglobal() {
		return hasRight(admglobal, 0);
	}

	public boolean isAdmrsys() {
		return hasRight(admsystem, 0);
	}

	public boolean isAdmrself() {
		return hasRight(admself, 0);
	}

	public boolean isDelrglobal() {
		return hasRight(admglobal, 0) || hasRight(admglobal, 4);
	}

	public boolean isDelrself() {
		return hasRight(admself, 0) || hasRight(admself, 4);
	}

	public boolean isDelrsys() {
		return hasRight(admsystem, 0) || hasRight(admsystem, 4);
	}

	public boolean isEditrglobal() {
		return hasRight(admglobal, 0) || hasRight(admglobal, 3);
	}

	public boolean isEditrself() {
		return hasRight(admself, 0) || hasRight(admself, 3);
	}

	public boolean isEditrsys() {
		return hasRight(admsystem, 0) || hasRight(admsystem, 3);
	}

	public boolean isViewrglobal() {
		return hasRight(admglobal, 0) || hasRight(admglobal, 1);
	}

	public boolean isViewrself() {
		return hasRight(admself, 0) || hasRight(admself, 1);
	}

	public boolean isViewrsys() {
		return hasRight(admsystem, 0) || hasRight(admsystem, 1);
	}

	// 是否全部系统
	public boolean isAllrsys() {
		return hasRight(admsystem, 5);
	}

	/**
	 * 判断权限字符串的指定位置是否有权限
	 *
	 * @param rigstr
	 *            权限字符串，增删改查，分别用1和0表示，顺序表示 管理-查看-增加-修改-删除
	 * @param ipos
	 * @return
	 */
	protected boolean hasRight(String rigstr, int ipos) {
		if (rigstr == null)
			return false;
		if (rigstr.length() - 1 < ipos)
			return false;

		return (rigstr != null && rigstr.length() > ipos && rigstr.charAt(ipos) == '1');
	}

	public String getIncludeorg() {
		return includeorg;
	}

	public void setIncludeorg(String includeorg) {
		this.includeorg = includeorg;
	}

}
